package io.cici.cc.mybatis.plus.core.injector.methods;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.lite.mapping.SqlSource;
import io.cici.cc.mybatis.plus.core.enums.SqlMethod;
import io.cici.cc.mybatis.plus.core.injector.Method;
import io.cici.cc.mybatis.plus.core.metadata.TableInfo;

/**
 * 根据 ID 更新有值字段
 */
public class UpdateById extends Method {

    public UpdateById() {
        this(SqlMethod.UPDATE_BY_ID.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public UpdateById(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.UPDATE_BY_ID;

        String sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(),

                tableInfo.getKeyColumn(), ENTITY_DOT + tableInfo.getKeyProperty());
        SqlSource sqlSource = super.createSqlSource(configuration, sql, modelClass);
        return addUpdateMappedStatement(mapperClass, modelClass, methodName, sqlSource);
    }
}
